package it.userService;



import it.Dao.UserbookDao;
import it.domain.UBPageBean;
import it.domain.Userbook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wsx
 * @create 2020-04-15-17:10
 */
@WebServlet("/borrowhistory")
public class borrowhistory extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession();
        String user = (String) session.getAttribute("user");
        String currentPages = req.getParameter("currentPage");
        //System.out.println(currentPages);
        if (currentPages == null || "".equals(currentPages)) {
            currentPages = "1";
        }
        Integer currentPage = Integer.valueOf(currentPages);
        //防止点上一页时越界
        if (currentPage <= 1) {
            currentPage = 1;
        }
        UBPageBean ubPageBean = new UBPageBean();
        UserbookDao userbookDao = new UserbookDao();
        Long totalCount = null;
        try {
            totalCount = userbookDao.getATotalCount(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //System.out.println(totalCount);
        //总记录数
        ubPageBean.setTotalCount(totalCount.intValue());
        //每页的记录数
        Integer counts = 8;
        //总页数
        double totalPage = Math.ceil(1.0 * ubPageBean.getTotalCount() / counts);
        if (currentPage >= (int) totalPage) {
            currentPage = (int) totalPage;
        }
        ubPageBean.setCurrPage(currentPage);
        ubPageBean.setTotalPage((int) totalPage);
        //记录详细信息
        List<Userbook> list = null;
        try {
            list = userbookDao.getAUBList(user, currentPage, counts);
            for (Object o:list){
                System.out.println(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //System.out.println(list);
        ubPageBean.setBooksList(list);
        req.setAttribute("ubPageBean", ubPageBean);
        req.getRequestDispatcher("reader_history.jsp").forward(req, resp);
//        System.out.println("5");
    }
}
